<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>员工管理</title>
</head>
<body class="hold-transition skin-black sidebar-mini">
<div th:replace="common/fragment :: link"></div>
<div class="wrapper">
    <div th:replace="common/fragment :: navbar"></div>
    <div th:replace="common/fragment :: menu"></div>
    <div class="content-wrapper">
        <section class="content-header">
            <h1>员工管理</h1>
        </section>
        <section class="content">
            <div class="box">
                <!--高级查询--->
                <div style="margin: 10px;">
                    <form class="form-inline" id="searchForm" action="/employee/list" method="post">
                        <input type="hidden" name="currentPage" id="currentPage" value="1">
                        <div class="form-group">
                            <label for="keyword">关键字:</label>
                            <input type="text" class="form-control" th:value="${qo.keyword}" id="keyword" name="keyword"
                                   placeholder="请输入姓名/邮箱">
                        </div>
                        <div class="form-group">
                            <label for="dept"> 部门:</label>
                            <select class="form-control" id="dept" name="deptId">
                                <option value="-1">全部</option>
                                <option th:selected="${d.id == qo.deptId}" th:each="d : ${departments}"
                                        th:text="${d.name}" th:value="${d.id}"></option>
                            </select>
                        </div>
                        <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span>
                            查询
                        </button>
                        <a href="/employee/input" class="btn btn-success btn-input">
                            <span class="glyphicon glyphicon-plus"></span> 添加
                        </a>
                        <a href="javascript:void(0)" class="btn btn-success btn-input" onclick="exportOP()">
                            <span class="glyphicon glyphicon-plus"></span> 导出
                        </a>

                        <a href="javascript:void(0)" class="btn btn-success btn-input" onclick="importOP()">
                            <span class="glyphicon glyphicon-plus"></span> 导入
                        </a>
                    </form>
                </div>
                <div class="box-body table-responsive ">
                    <table class="table table-hover table-bordered table-striped">
                        <thead>
                        <tr>
                            <th><input type="checkbox" id="allCb"></th>
                            <th>编号</th>
                            <th>用户名</th>
                            <th>真实姓名</th>
                            <th>邮箱</th>
                            <th>年龄</th>
                            <th>部门</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <tr th:each="e:${pageInfo.list}">
                            <td><input type="checkbox" class="cb"></td>
                            <td th:text="${e.id}">1</td>
                            <td th:text="${e.username}">zs</td>
                            <td th:text="${e.name}">张三</td>
                            <td th:text="${e.email}">zs@126.com</td>
                            <td th:text="${e.age}">21</td>
                            <td th:text="${e.dept?.name}">开发部</td>
                            <td>
                                <a class="btn btn-info btn-xs btn_redirect" th:href="|/employee/input?id=${e.id}|">
                                    <span class="glyphicon glyphicon-pencil"></span> 编辑
                                </a>
                                <a class="btn btn-danger btn-xs btn-delete" th:data-url="|/employee/delete?id=${e.id}|">
                                    <span class="glyphicon glyphicon-trash"></span> 删除
                                </a>
                            </td>
                        </tr>
                        </tbody>
                    </table>
                    <div th:replace="common/fragment :: page"></div>
                </div>
            </div>
        </section>
    </div>
    <div th:replace="common/fragment :: footer"></div>
</div>
<div class="modal fade" id="importModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                    <tton>
                        <h4 class="modal-title" id="myModalLabel">导入</h4>
            </div>
            <form class="form-horizontal" enctype="multipart/form-data" method="post" id="importForm">
                <div class="modal-body">
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="name" class="col-sm-3 control-label"></label>
                        <div class="col-sm-6">
                            <!-- 文件上传框 -->
                            <input id="uploadFile" type="file" name="file"/>
                        </div>
                    </div>
                    <div class="form-group" style="margin-top: 10px;">
                        <div class="col-sm-3"></div>
                        <div class="col-sm-6">
                            <a href="#" onclick="downloadTemplateOP()" class="btn btn-success">
                                <span class="glyphicon glyphicon-download"></span> 下载模板
                            </a>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消
                    </button>
                    <button type="button" onclick="importSave()" class="btn btn-primary btn-submit">保存
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    function exportOP() {
        window.open("/employee/download?" + $("#searchForm").serialize());
    }

    function importOP() {
        $("#uploadFile").val("");
        $("#importModal").modal("show");
    }

    function downloadTemplateOP() {
        window.open("/employee/template")
    }

    function importSave() {
        let $file1 = $("#uploadFile").val();//用户文件内容(文件)
        // 判断文件是否为空
        if ($file1 == "") {
            Swal.fire({
                text: "请选择上传的目标文件! ",
                icon: 'warning',
            })
            return false;
        }

        let formData = new FormData();//这里需要实例化一个FormData来进行文件上传
        formData.append("file", $("#uploadFile")[0].files[0]);
        console.log(formData)
        $.ajax({
            type: "post",
            url: "/employee/importExcel",
            data: formData,
            processData: false,
            contentType: false,
            success: function (data) {
                if (data.success) {
                    Swal.fire({
                        text: data.msg,
                        icon: 'success',
                    })
                    $("#importModal").modal("hide");
                    $('#table').bootstrapTable('refresh');
                } else {
                    Swal.fire({
                        text: data.msg,
                        icon: 'error',
                    })
                }
            }
        });
    }

</script>
</body>
</html>